package com.huijiet.base.component;

import com.huijiet.base.common.CommonParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MySQLPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if(bean instanceof DataSourceProperties){
            DataSourceProperties p = (DataSourceProperties) bean;
            changeMySQL(p);
        }

        return bean;
    }

    private void changeMySQL(DataSourceProperties p){
        if(CommonParam.MYSQL_NEED_CHANGE){
            log.info("need modify mysql info");
            p.setUrl(CommonParam.MYSQL_URL);
            p.setUsername(CommonParam.MYSQL_USERNAME);
            p.setPassword(CommonParam.MYSQL_PASSWORD);
        }else {
            log.info("no need modify mysql info");
        }
    }
}
